package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.SeekBar;

import com.hanlink.dsp.util.Constants;
import com.hanlink.dsp.util.DspUtil;
import com.qztech.sdk.R;
import com.qztech.sdk.model.SettingConstants;


/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015-11-16
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015-11-16        PeterVon       初始创建
 *
 * </pre>
 */
public class EqChannelView extends FrameLayout {

    VerticalSeekBar vseekBar;
    private boolean isClickable = true;
    private ImageButton mIbtnPlus;
    private ImageButton mIbtnSub;

    private RadioButton mRbFreqValue;
    private RadioButton mRbQValue;
    private RadioButton mTxtValue;

    private OnSeekBarCallbacks mOnSeekBarCallbacks;

    private OnCheckChangedCallback mOnCheckChangedCallback;

    private long mLastChangeTs;

//	private int currentIndex;

    private int page;

    public EqChannelView(Context context) {
        super(context);
        initView(context);
    }

    public EqChannelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public EqChannelView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }


    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(com.hanlink.res.R.layout.view_eq_channel_bar, this, true);
        mTxtValue = view.findViewById(R.id.txt_value);
        mRbQValue = view.findViewById(com.hanlink.res.R.id.txt_q_value);
        mIbtnPlus = view.findViewById(R.id.ibtn_plus);
        mIbtnSub = view.findViewById(R.id.ibtn_sub);
        mRbFreqValue = view.findViewById(R.id.txt_hz_value);
        vseekBar = view.findViewById(com.hanlink.res.R.id.vSeekbar);

        mTxtValue.setOnCheckedChangeListener(mCheckedChangeListener);
        mRbQValue.setOnCheckedChangeListener(mCheckedChangeListener);
        mRbFreqValue.setOnCheckedChangeListener(mCheckedChangeListener);

        vseekBar.setMax(SettingConstants.GAIN_MAX - SettingConstants.GAIN_MIN);
        vseekBar.setProgress((SettingConstants.GAIN_MAX - SettingConstants.GAIN_MIN) / 2);
        vseekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mTxtValue.setText(DspUtil.gainToValue(progress));
                if (mOnSeekBarCallbacks != null) {
                    if (System.currentTimeMillis() - mLastChangeTs > Constants.SEEK_BAR_DELAY) {
                        mLastChangeTs = System.currentTimeMillis();
                        mOnSeekBarCallbacks.onProgressChanged(seekBar, progress, (Integer) EqChannelView.this.getTag(), fromUser);
                    }
                }
                if (!mTxtValue.isChecked()) {
                    setChecked(true);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isClickable = false;
                if (!mTxtValue.isChecked()) {
                    setChecked(true);
                }

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isClickable = true;
                mTxtValue.setText(DspUtil.gainToValue(seekBar.getProgress()));
                if (mOnSeekBarCallbacks != null) {
                    mOnSeekBarCallbacks.onProgressChangeOver(seekBar, seekBar.getProgress(), (Integer) EqChannelView.this.getTag());
                }
            }
        });

        mIbtnSub.setOnClickListener(onClickListener);
        mIbtnPlus.setOnClickListener(onClickListener);
    }


    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isClickable) return;
            int progress = vseekBar.getProgress();
            int id = v.getId();
            if (id == R.id.ibtn_plus) {
                if (progress < vseekBar.getMax()) {
                    vseekBar.setTag(true);
                    vseekBar.setProgress(progress + 1);
                }
            } else if (id == R.id.ibtn_sub) {
                if (progress > 0) {
                    vseekBar.setTag(true);
                    vseekBar.setProgress(progress - 1);
                }
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                setChecked(true);
                if (mTxtValue == buttonView && mOnCheckChangedCallback != null) {
                    mOnCheckChangedCallback.onChecked(page, (Integer) EqChannelView.this.getTag(), isChecked);
                }
            }
        }
    };


    public void setEnabled(boolean enabled) {
        vseekBar.setEnabled(enabled);
        mIbtnPlus.setEnabled(enabled);
        mIbtnSub.setEnabled(enabled);
        mTxtValue.setEnabled(enabled);
        mRbFreqValue.setEnabled(enabled);
        mRbQValue.setEnabled(enabled);
    }


    public void setQ(float q) {
        if (mRbQValue != null) {
            mRbQValue.setText(DspUtil.qToQValue(q));
        }
    }

    /**
     * 设置选中后的数值
     */
    public void setValue(float q, int gain, int freq) {
        if (mTxtValue != null) {
            mTxtValue.setText(DspUtil.gainToValue(gain));
        }
        setQ(q);
        setHzValue(freq);
        vseekBar.setProgress(gain);
    }

    public void setOnSeekBarCallbacks(OnSeekBarCallbacks mOnSeekBarCallbacks) {
        this.mOnSeekBarCallbacks = mOnSeekBarCallbacks;
    }

    @Override
    public void setTag(Object tag) {
        super.setTag(tag);
        vseekBar.setTag(tag);
    }

    public void init(int page, Object tag, int hzValue, OnSeekBarCallbacks callbacks) {
        this.page = page;
        this.setTag(tag);
        this.setHzValue(hzValue);
        this.setOnSeekBarCallbacks(callbacks);
    }

    public void setHzValue(int value) {
        String result;
        result = String.valueOf(value);
        mRbFreqValue.setText(result);
    }

    public void setChecked(boolean isChecked) {
        mTxtValue.setChecked(isChecked);
        mRbQValue.setChecked(isChecked);
        mRbFreqValue.setChecked(isChecked);
    }

    public void setOnCheckChangedCallback(OnCheckChangedCallback mOnCheckChangedCallback) {
        this.mOnCheckChangedCallback = mOnCheckChangedCallback;
    }

    public interface OnCheckChangedCallback {
        void onChecked(int page, int currentIndex, boolean isChecked);
    }

    public interface OnSeekBarCallbacks {

        //void onStopTrackingTouch(SeekBar seekBar, int currentIndex);

        void onProgressChanged(SeekBar seekBar, int progress, int currentIndex, boolean sendCmd);

        void onProgressChangeOver(SeekBar seekBar, int progress, int currentIndex);
    }
}
